package com.laozhao.wintersystem;

import java.awt.BorderLayout;
import java.awt.Image;

import javax.swing.JFrame;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.laozhao.component.DefaultFrame;

public class WFrameModel {

//	private Map<String, ?extends JFrame> framesMap = new HashMap<String, JFrame>();
	private JFrame mainFrame;
	private WListenersModel listenersModel ;
	private WMenubarModel menuModel;
	private WTasksModel tasksModel;
	private WPagesModel pagesModel;

	private final String ELEMENT_FRAME_LISTENERS = "listeners"; 
	private final String ELEMENT_FRAME_MENUBAR = "menubar"; 
	private final String ELEMENT_FRAME_TASKS = "tasks"; 
	private final String ELEMENT_FRAME_PAGES = "pages"; 

	private final String ATTLIST_FRAME_ID = "id"; 
	private final String ATTLIST_FRAME_CLASS = "class"; 
	private final String ATTLIST_FRAME_NAME = "name"; 
	private final String ATTLIST_FRAME_IMG = "img"; 
	private final String ATTLIST_FRAME_TIP = "tip";
	private final String ATTLIST_FRAME_EXIT = "exit";
	
	String tmpId ;
	String tmpClass ;
	String tmpName ;
//	Icon tmpIcon;;
	Image tmpImg;
	String tmpTip;
	int tmpExit = JFrame.DO_NOTHING_ON_CLOSE;
	
	public WFrameModel(Node node) {

		if (node != null) {
			tmpId = WSTools.getStringAttribute(node, ATTLIST_FRAME_ID);
			tmpClass = WSTools.getStringAttribute(node, ATTLIST_FRAME_CLASS);
			tmpName = WSTools.getStringAttribute(node, ATTLIST_FRAME_NAME);
			tmpImg = WSTools.getImageAttribute(node, ATTLIST_FRAME_IMG);
			tmpTip = WSTools.getStringAttribute(node, ATTLIST_FRAME_TIP);
			tmpExit = WSTools.getIntAttribute(node, ATTLIST_FRAME_EXIT);
			tmpExit = tmpExit == 0 ? JFrame.DO_NOTHING_ON_CLOSE : tmpExit;
			if (tmpClass == null || "".equals(tmpClass)) {
				mainFrame = new DefaultFrame(tmpName,tmpImg,tmpExit);
			}else {
				try {
					Object xObject = Class.forName(tmpClass).newInstance();
					if (xObject instanceof JFrame) {
						mainFrame = (JFrame) xObject;
						mainFrame.setTitle(tmpName);
						mainFrame.setIconImage(tmpImg);
						mainFrame.setDefaultCloseOperation(tmpExit);
					}else {
						throw new RuntimeException("frames class must extends JFrame");
					}
				} catch (InstantiationException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			NodeList nodes = node.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) {
				org.w3c.dom.Node child = nodes.item(i);
				if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
					if (child.getNodeName().equalsIgnoreCase(ELEMENT_FRAME_LISTENERS)) {
						listenersModel = new WListenersModel(node);
					}else if (child.getNodeName().equalsIgnoreCase(ELEMENT_FRAME_MENUBAR)) {
						menuModel = new WMenubarModel(node);
					}else if (child.getNodeName().equalsIgnoreCase(ELEMENT_FRAME_TASKS)) {
						 tasksModel = new WTasksModel(node);
					}else if (child.getNodeName().equalsIgnoreCase(ELEMENT_FRAME_PAGES)) {
						pagesModel = new WPagesModel(node);
					}
				}
			}
			buildFrame(mainFrame , menuModel ,tasksModel , pagesModel );
//			frames.put(tmpId, mainFrame);
//			actionsModel.put(tmpId, tmpActions);
//			listenersModel.put(tmpId,tmpListeners );
//			menuModel.put(tmpId, tmpMenubar);
//			tasksModel.put(tmpId, tmpTaskContainer);
//			pagesModel.put(tmpId, tmpPageContainer);
		}
	}

	private void buildFrame(JFrame frame, WMenubarModel menubar,
			WTasksModel taskContainer, WPagesModel pageContainer) {
		frame.getContentPane().setLayout(new BorderLayout(5,5));
		frame.getContentPane().add(menubar.getMenuBar(),BorderLayout.NORTH);
		frame.getContentPane().add(taskContainer.getTaskContainer(),BorderLayout.WEST);
		frame.getContentPane().add(pageContainer.getPageContainer(),BorderLayout.CENTER);
	}
}
